/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package oldskool1;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Arrays;
import vectorsdk.Vector3;

public class Vectorballs {
    
    private Vector3[] pos;
    private Vector3[] trans;
    private Vector3[] normal;
    
    public Vectorballs(int amount) {
        pos = new Vector3[amount];
        trans = new Vector3[amount];
        normal = new Vector3[amount];
        for(int i=0; i<amount; i++) {
            pos[i] = new Vector3(0.0, 0.0, 0.0);
            trans[i] = new Vector3(0.0, 0.0, 0.0);
            normal[i] = trans[i].normalize();
        }
    }

    public int numBalls() {
        return pos.length;
    }
    
    public void rotate(double xAngle, double yAngle) {
        xAngle = Math.toRadians(xAngle);
        yAngle = Math.toRadians(yAngle);
        for(int i=0; i<pos.length; i++) {
            // X Rotation
            trans[i].setPos( pos[i].x(), pos[i].y() * Math.cos(xAngle) - pos[i].z() * Math.sin(xAngle),
                    pos[i].y() * Math.sin(xAngle) + pos[i].z() * Math.cos(xAngle));
            // Y Rotation
            trans[i].setPos( pos[i].x() * Math.cos(yAngle) + trans[i].z() * Math.sin(yAngle), trans[i].y(),
                    trans[i].x() * -Math.sin(yAngle) + trans[i].z() * Math.cos(yAngle));
        }
    }
    
    public void posBall(int i, double X, double Y, double Z) {
        pos[i].setPos(X, Y, Z);
        trans[i] = pos[i].copy();
    }
    
    private double scaleValue(double v, double oldMax, double oldMin, double newMax, double newMin) {
        return (v / ((oldMax - oldMin) / (newMax - newMin))) + newMin;
    }
    
    public void draw(Graphics g, int ScreenW, int ScreenH, double pov, double xAngle, double yAngle) {
        g.setColor(Color.white);
        int SW = ScreenW / 2;
        int SH = ScreenH / 2;
        rotate(xAngle, yAngle);
        Arrays.sort(trans);
        for(int i=0; i<trans.length; i++) {
            double z = trans[i].z() + 15.0;
            if( z > 1.0 ) {
                double px = trans[i].x() / z;
                double py = trans[i].y() / (z / pov);
                int sx = (int)(px * SW + SW);
                int sy = (int)(-py * SH + SH);
                int c = 255 - (int)scaleValue(z, 22.5, 1.0, 255.0, 0.0);
                int c2 = c*2;
                if(c < 0) c = 0;
                if(c > 255) c = 255;
                if(c2 < 0) c2 = 0;
                if(c2 > 255) c2 = 255;
                int s = 48 - (int)scaleValue(z, 32.5, 1.0, 48.0, 2.0);
                int s2 = (s >> 1);
                int s8 = (s >> 3);
                g.setColor(new Color(c, 0, 0));
                g.fillOval(sx-s2, sy-s2, s, s);
                g.setColor(new Color(c2, c>>1, c>>1));
                g.fillOval(sx-s2+s8, sy-s2+s8, s2, s2);
                g.setColor(Color.BLACK);
                g.drawOval(sx-s2, sy-s2, s, s);
            }
        }
    }
}
